﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint.Administration;

namespace Mystery.SharePoint
{
	public static class SPQuotaTemplateCollectionExtensions
	{
		/// <summary>
		/// Registers a new quota template
		/// </summary>
		/// <param name="templates">Template collection</param>
		/// <param name="name">Name of template</param>
		/// <param name="storageMax">Maximum amount of storage allocated. In MegaBytes</param>
		/// <param name="storageWarning">When storage passes this value, a warning is triggered</param>
		public static void Register(this SPQuotaTemplateCollection templates,
			string name, long storageMax, long storageWarning)
		{
			if (templates == null) throw new ArgumentNullException("templates");
			if (string.IsNullOrEmpty(name) == true) throw new ArgumentNullException("name");

			templates.Register(name, storageMax, storageWarning, 300.0, 200.0);
		}
		/// <summary>
		/// Registers a new quota template
		/// </summary>
		/// <param name="templates">Template collection</param>
		/// <param name="name">Name of template</param>
		/// <param name="storageMax">Maximum amount of storage allocated. In MegaBytes</param>
		/// <param name="storageWarning">When storage passes this value, a warning is triggered</param>
		/// <param name="userCodeMax">Maximum number of points allocated to sandboxed solutions</param>
		/// <param name="userCodeWarning">When number of points passes this value, a warning is triggered</param>
		public static void Register(this SPQuotaTemplateCollection templates,
			string name, long storageMax, long storageWarning, double userCodeMax, double userCodeWarning)
		{
			if (templates == null) throw new ArgumentNullException("templates");
			if (string.IsNullOrEmpty(name) == true) throw new ArgumentNullException("name");

			templates.Register(name, storageMax, storageWarning, userCodeMax, userCodeWarning, 0);
		}
		/// <summary>
		/// Registers a new quota template
		/// </summary>
		/// <param name="templates">Template collection</param>
		/// <param name="name">Name of template</param>
		/// <param name="storageMax">Maximum amount of storage allocated. In MegaBytes</param>
		/// <param name="storageWarning">When storage passes this value, a warning is triggered</param>
		/// <param name="userCodeMax">Maximum number of points allocated to sandboxed solutions</param>
		/// <param name="userCodeWarning">When number of points passes this value, a warning is triggered</param>
		/// <param name="invitedUsers">Number of users that can be invited</param>
		public static void Register(this SPQuotaTemplateCollection templates,
			string name, long storageMax, long storageWarning, double userCodeMax, double userCodeWarning, int invitedUsers)
		{
			if (templates == null) throw new ArgumentNullException("templates");
			if (string.IsNullOrEmpty(name) == true) throw new ArgumentNullException("name");

			int bytesPerMB = 1048576;

			SPQuotaTemplate template = templates[name];
			if (template == null)
			{
				template = new SPQuotaTemplate();
			}
			template.Name = name;
			template.StorageMaximumLevel = storageMax * bytesPerMB;
			template.StorageWarningLevel = storageWarning * bytesPerMB;
			template.UserCodeMaximumLevel = userCodeMax;
			template.UserCodeWarningLevel = userCodeWarning;
			template.InvitedUserMaximumLevel = invitedUsers;

			Logger.L.AddInformation(Category.Administration,
						"Creating quota template:{0} StorageWarningLevel:{1} StorageMaximumLevel:{2} UserCodeWarningLevel:{3} UserCodeMaximumLevel: {4} InvitedUserMaximumLevel:{5}",
						template.Name, 
						template.StorageWarningLevel,
						template.StorageMaximumLevel,
						template.UserCodeWarningLevel, 
						template.UserCodeMaximumLevel, 
						template.InvitedUserMaximumLevel);

			templates.Add(template);
		}
	}
}
